import { ref, computed } from 'vue'
import {createPinia, defineStore} from 'pinia'
import Cookies from 'js-cookie'
import request from "@/utils/axiosUtils.js";
import router from "@/router/index.js";
import piniaPluginPersistedstate from 'pinia-plugin-persistedstate'
export const user = defineStore('user', () => {
  const userToken=()=>{
    return Cookies.get('doctor_token')
  };
  const userId=ref('');
  const userName=ref('');
  const avatar=ref('');
  const nikcname=ref('');
  const phone=ref('');
  const email=ref('');
  const city=ref('');
  const realName=ref('');
  const sex=ref('');
  const identityCard=ref('');
  const birthday=ref('');


  const roles=ref([]);
  const permissions=ref([]);
  const menus=ref([]);
  function login(form){
    return new Promise((resolve, reject) => {
      request({
        url: '/login',
        method: 'post',
        data: form
      }).then(res => {
        getInfo().finally(()=>{
          resolve(res)
        })
      }).catch(error => {
        reject(error)
      })
    })
  }
  function logout() {
    return new Promise((resolve, reject) => {
      request({
        url: '/logout',
        method: 'post'
      }).then((res) => {
        Cookies.remove('doctor_token');
        resolve(res);
        userId.value = '';
        userName.value = '';
        avatar.value = '';
        roles.value = [];
        permissions.value = [];
        nikcname.value = '';
        phone.value = '';
        email.value = '';
        city.value = '';
        realName.value = '';
        sex.value = '';
        identityCard.value = '';
        birthday.value = '';
        resolve(res);
      }).catch(error => {
        reject(error);
      })
    })
  }
  function getInfo(){
    return new Promise((resolve, reject) => {
      request({
        url: '/userInfo',
        method: 'get'
      }).then(res => {
        const userDTO = res.data.user;
        userId.value = userDTO.id;
        userName.value = userDTO.username;
        avatar.value = userDTO.avatar;
        nikcname.value = userDTO.nickname;
        phone.value = userDTO.phone;
        email.value = userDTO.email;
        city.value = userDTO.city;
        roles.value = userDTO.roles;
        permissions.value = userDTO.authorities;
        menus.value = userDTO.menus;

        const userInfo=res.data.usersInfo;

        realName.value = userInfo.realname;
        sex.value = userInfo.sex;
        identityCard.value = userInfo.identitycard;
        birthday.value = userInfo.birthday;
        resolve(res)
      }).catch(error => {
        reject(error)
      })
    })
  }
  function register(user){
    return new Promise((resolve, reject) => {
      request({
        url: '/register',
        method: 'post',
        data: user
      }).then(res => {
        resolve(res)
      }).catch(error => {
        reject(error)
      })
    })
  }
  return {
    userToken,
    userId,
    userName,
    avatar,
    roles,
    permissions,
    menus,
    login,
    logout,
    getInfo,
    register,
    nikcname,
    phone,
    email,
    city,
    realName,
    sex,
    identityCard,
    birthday
  }
})
export const dynamicRoutes = defineStore('dynamicRoutes', () => {
      const routes = ref([])
      const addRoutes = (routes) => {
        routes.forEach(route => {
          router.addRoute(route)
        })
      }
      return {
        routes,
        addRoutes
      }
    }
)
export const pinia = createPinia();
//pinia.use(piniaPluginPersistedstate);

